package com.sn.system.common.validator;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.sn.system.common.constants.Constants;

public class Validator {

    public static boolean required(String str) {
        return (str!=null&&!str.equals(""));
    }
    
    public static boolean fullText(String str) {
        return regexValidator(str,ValidatorUtils.NotHalfKatakana);
    }
    public static boolean hiraText(String str,int length ) {
    	if(str==null){
    		return true;
    	}
        return (str.length()<=length)&&regexValidator(str,ValidatorUtils.NotHalfHirakana);
    }
    public static boolean date(String str) {
    	if(str==null||str.equals("")){
    		return true;
    	}
    	if(str.length()<10||str.length()>7){
    		String[] dates=str.split("\\/");
    		if(dates.length==3){
    			str=dates[0]+"/"+(dates[1].length()==2?dates[1]:"0"+dates[1])+"/"+(dates[2].length()==2?dates[2]:"0"+dates[2]);
    		}
    	}
        return regexValidator(str,ValidatorUtils.DateExpression);
    }
    public static boolean birthday(String str) {
    	boolean ok=false;
    	if(str==null||str.equals("")){
    		return true;
    	}
    	if(str.length()<10||str.length()>7){
    		String[] dates=str.split("\\/");
    		if(dates.length==3){
    			str=dates[0]+"/"+(dates[1].length()==2?dates[1]:"0"+dates[1])+"/"+(dates[2].length()==2?dates[2]:"0"+dates[2]);
    		}
    	}
    	if(regexValidator(str,ValidatorUtils.DateExpression)){
    		ok=Integer.parseInt(str.replace("/", ""))<=Integer.parseInt(Constants.DATE_FORMAT4.format(new Date()));
    	}
        return ok;
    }
    public static boolean rangeDate(String date1,String date2) {
    	boolean ok=true;
    	if(date1==null||date1.equals("")||date2==null||date2.equals("")){
    		return ok;
    	}
    	if(regexValidator(date1,ValidatorUtils.DateExpression)&&regexValidator(date2,ValidatorUtils.DateExpression)){
    		ok=Integer.parseInt(date1.replace("/", ""))<=Integer.parseInt(date2.replace("/", ""));
    	}
        return ok;
    }
    public static boolean email(String str) {
        return regexValidator(str,ValidatorUtils.emailAddressPattern);
    }
    public static boolean email(String str,int length ) {
    	if(str==null){
    		return true;
    	}
        return (str.length()<=length)&&regexValidator(str,ValidatorUtils.emailAddressPattern);
    }
    public static boolean zipcode(String str) {
    	if(str==null){
    		return true;
    	}
        return (str.length()<=7)&&regexValidator(str,ValidatorUtils.HalfNumeric);
    }  
    public static boolean notHalfkana(String str,int length ) {
    	if(str==null){
    		return true;
    	}
        return (str.length()<=length)&&regexValidator(str,ValidatorUtils.NotHalfKatakana);
    }
    public static boolean notHalfText(String str,int length ) {
    	if(str==null){
    		return true;
    	}
        return (str.length()<=length)&&regexValidator(str,ValidatorUtils.NotHalfString);
    }
    public static boolean kanzisimei(String str,int length ) {
    	if(str==null){
    		return true;
    	}
        return (str.length()<=length)&&regexValidator(str,ValidatorUtils.Kanzisimei);
    }
    public static boolean stringlength(String str,int length) {
    	if(str==null){
    		return true;
    	}
        return str.length()<=length;
    }
    public static boolean sex(String str) {
    	return regexValidator(str,ValidatorUtils.sex);
    }
    public static boolean websb(String str) {
    	return regexValidator(str,ValidatorUtils.sex);
    }
    public static boolean numText(String str,int length) {
    	if(str==null){
    		return true;
    	}
        return (str.length()<=length)&&regexValidator(str,ValidatorUtils.HalfNumeric);
    }
    public static boolean hafeText(String str,int length) {
    	if(str==null){
    		return true;
    	}
        return (str.length()<=length)&&regexValidator(str,ValidatorUtils.HalfString);
    } 
    public static boolean telephone(String str) {
        return regexValidator(str,ValidatorUtils.telephone);
    }
    public static boolean mobile(String str) {
        return regexValidator(str,ValidatorUtils.mobile);
    }
    public static boolean fax(String str) {
        return regexValidator(str,ValidatorUtils.fax);
    }
     
    private static boolean regexValidator(String str, String expression) {
    	if(str==null||str.equals("")){
    		return true;
    	}else{
    		 Pattern pattern;
    	        // System.out.println(expression);
    	        pattern = Pattern.compile(expression, 2);
    	        Matcher matcher = pattern.matcher(str.trim());
    	        if (!matcher.matches()) {
    	            return false;
    	        }
    	    return true;
    	}
       
    }
}
